import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/Login.vue')
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import('../views/Detail/Detail.vue')
  },
  {
    path: '/txsq',
    name: 'txsq',
    component: () => import('../views/Txsq/Txsq.vue')
  },
  {
    path: '/jbsq',
    name: 'jbsq',
    component: () => import('../views/Jbsq/Jbsq.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
/* 注册全局导航守卫，来判断是否已经登录 */
router.beforeEach((to, from, next) => {
 var token= localStorage.getItem('token')
  if(token){//已登录
    if(to.path==='/login'){
      next('/home')
    }else{
      next()
    }
  }else{//未登录
    if(to.path==='/login'){
      next() 
    }else{
      next('/login')
    }
  }
})
export default router
